#include "mainwindow.h"

#include "myformdoc.h"
#include "qpainter.h"


#include "myformtable.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindowClass())
{
    ui->setupUi(this);

    setCentralWidget(ui->tabWidget);

    ui->tabWidget->setVisible(false);
    ui->tabWidget->clear();
    ui->tabWidget->setTabsClosable(true);//给个关闭的按钮，点击后信号tabCloseRequested发出
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.drawPixmap(0, ui->mainToolBar->height(),
        width(), height() - ui->mainToolBar->height() - ui->statusBar->height(),
        QPixmap(":/MainWindow/resource/圣诞海报640X300.jpg"));
}


void MainWindow::on_action_WidgetInside_triggered()
{
    MyFormDoc* form_doc = new MyFormDoc(this);
    form_doc->setAttribute(Qt::WA_DeleteOnClose);
    int cur = ui->tabWidget->addTab(form_doc,
        QString::asprintf("Doc %d", ui->tabWidget->count()));
    ui->tabWidget->setCurrentIndex(cur);
    ui->tabWidget->setVisible(true);

    connect(form_doc, &MyFormDoc::title_changed,
        this, &MainWindow::do_change_tab_title);
}


void MainWindow::do_change_tab_title(QString str)
{
    int index = ui->tabWidget->currentIndex();
    ui->tabWidget->setTabText(index, str);
}

void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
    ui->tabWidget->widget(index)->close();
}

void MainWindow::on_action_Widget_triggered()
{
    MyFormDoc* form_doc = new MyFormDoc();
    form_doc->setAttribute(Qt::WA_DeleteOnClose);
    form_doc->setWindowTitle("基于QWidget窗口");
    form_doc->setWindowOpacity(0.8);//不透明度
    form_doc->show();

}

void MainWindow::on_action_WindowInside_triggered()
{
    MyFormTable* form_table = new MyFormTable(this);
    form_table->setAttribute(Qt::WA_DeleteOnClose);
    int cur = ui->tabWidget->addTab(form_table,
        QString::asprintf("Tab %d", ui->tabWidget->count()));
    ui->tabWidget->setCurrentIndex(cur);
    ui->tabWidget->setVisible(true);

}

void MainWindow::on_action_Window_triggered()
{
    MyFormTable* form_table = new MyFormTable();
    form_table->setAttribute(Qt::WA_DeleteOnClose);
    form_table->setWindowTitle("独立的Window");
    form_table->show();
}
